﻿using System;
using Microsoft.Win32;
using System.Collections;

namespace Bingrounder
{
    /// <summary>
    /// Summary description for MyReg.
    /// </summary>
    public class RegistryHelper
    {
        private RegistryKey rKey;
        private string sRootName;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Root">Your Application Name</param>
        public RegistryHelper(string Root)
        {
            sRootName = @"Software\" + Root;
            rKey = getRegKeyFromCurrentUser("");
            rKey.Close();
        }

        public object this[string name]
        {
            get
            {
                try
                {
                    int dLastIndex = name.LastIndexOf(@"\");
                    rKey = getRegKeyFromCurrentUser(name);
                    object temp;
                    if (dLastIndex != -1)
                        temp = rKey.GetValue(name.Substring(dLastIndex + 1, name.Length - dLastIndex - 1));
                    else
                        temp = rKey.GetValue(name);

                    rKey.Close();
                    return temp;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return "";
                }
            }
            set
            {
                try
                {
                    int dLastIndex = name.LastIndexOf(@"\");
                    rKey = getRegKeyFromCurrentUser(name);

                    if (dLastIndex != -1)
                        rKey.SetValue(name.Substring(dLastIndex + 1, name.Length - dLastIndex - 1), value);
                    else
                        rKey.SetValue(name, value);
                    rKey.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }

        private RegistryKey getRegKeyFromCurrentUser(string name)
        {
            try
            {
                RegistryKey tempRKey;
                int dLastIndex = name.LastIndexOf(@"\");
                if (dLastIndex != -1)
                    name = name.Substring(0, dLastIndex);
                else
                    name = "";
                tempRKey = Registry.CurrentUser.OpenSubKey(sRootName + "\\" + name, true);
                if (tempRKey == null)
                    tempRKey = Registry.CurrentUser.CreateSubKey(sRootName + "\\" + name);
                return tempRKey;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }

        private RegistryKey getRegKeyFromClassesRoot(string name)
        {
            try
            {
                RegistryKey tempRKey;
                int dLastIndex = name.LastIndexOf(@"\");
                if (dLastIndex != -1)
                    name = name.Substring(0, dLastIndex);
                else
                    name = "";
                tempRKey = Registry.ClassesRoot.OpenSubKey(sRootName + "\\" + name, true);
                if (tempRKey == null)
                    tempRKey = Registry.ClassesRoot.CreateSubKey(sRootName + "\\" + name);
                return tempRKey;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }
        /// <summary>
        /// Recent Files in Array List
        /// </summary>
        public ArrayList RecentFiles
        {
            set
            {
                try
                {
                    //delete the previous recent files
                    rKey = getRegKeyFromCurrentUser(@"");
                    rKey.DeleteSubKeyTree("Recent Files");
                    rKey.Close();

                    rKey = getRegKeyFromCurrentUser(@"Recent Files\a");
                    int i = 0;
                    foreach (string temp in (ArrayList)value)
                    {
                        rKey.SetValue("file" + i.ToString(), temp);
                        i++;
                    }
                    rKey.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            get
            {
                try
                {
                    ArrayList tempList = new ArrayList();

                    rKey = getRegKeyFromCurrentUser(@"Recent Files\a");
                    int i = 0;
                    string temp;
                    while ((temp = (string)rKey.GetValue("file" + i)) != null)
                    {
                        tempList.Add(temp);
                        i++;
                    }
                    rKey.Close();
                    return tempList;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return null;
                }
            }
        }

        /// <summary>
        /// Delete key
        /// </summary>
        public void Delete()
        {
            try
            {
                rKey = Registry.CurrentUser;
                rKey.DeleteSubKeyTree(sRootName);
                rKey.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// Save to registry, which Application to open your file
        /// </summary>
        /// <param name="extention">File extention</param>
        /// <param name="FileType">File Type</param>
        /// <param name="ApplicationPath">Application Path</param>
        public void SaveOpenWith(string extention, string FileType, string ApplicationPath)
        {
            try
            {
                rKey = getRegKeyFromClassesRoot("." + extention);
                rKey.SetValue("", FileType);
                rKey.Close();

                rKey = getRegKeyFromClassesRoot(FileType + "\\Shell\\open\\command");
                rKey.SetValue("", ApplicationPath + " %1");
                rKey.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
